package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerDetaildataImportInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/detaileddatareporting/")
public class CerDetaildataImportInfoController extends BaseController {

    @Autowired
    private CerDetaildataImportInfoService cerDetaildataImportInfoService;

    @PostMapping("listAllTableData")
    @ApiOperation("获取所有的详细数据信息列表")
    public JsonObj listByPage() {
        return cerDetaildataImportInfoService.listAllTableData(getPageData());
    }

    @PostMapping("queryAllExportData")
    @ApiOperation("点击导出按钮，查询出可以用来导出的详细数据源")
    public void queryAllExportData(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerDetaildataImportInfoService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
                .getData()
                .get("data");
        // 组装表头
        List<String> headList = Arrays.asList("姓名",
                "性别",
                "年龄",
                "身份证号",
                "地址",
                "联系电话",
                "门诊号",
                "住院号",
                "内镜诊疗日期",
                "对象来源",
                "内镜号",
                "是否无痛",
                "染色类别",
                "图像数量",
                "是否活检",
                "Hp检测结果",
                "Hp检测方法",
                "内镜诊断",
                "内镜诊断医师",
                "内镜审核医师",
                "病理号",
                "病理诊断",
                "食管病理诊断代码",
                "贲门病理诊断代码",
                "胃病理诊断代码",
                "十二指肠病理诊断代码",
                "病理诊断医师",
                "病理审核医师",
                "病理报告日期");
        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "详细数据导出", response);
        } catch (Exception e) {
        }
    }

    @PostMapping("batchDeletion")
    @ApiOperation("批量删除")
    public JsonObj batchDeletion() {
        PageData pageData = getPageData();
        return cerDetaildataImportInfoService.batchDeletion(pageData);
    }

    @GetMapping("oneClickDelete")
    @ApiOperation("一键删除")
    public JsonObj oneClickDelete() {
        PageData pageData = getPageData();
        return cerDetaildataImportInfoService.oneClickDelete(pageData);
    }

    @PostMapping("deleteById")
    public JsonObj deleteById() {
        PageData pageData = getPageData();
        return cerDetaildataImportInfoService.deleteById(pageData);
    }

    @GetMapping("findDataById")
    @ApiOperation("根据id获取详细信息")
    public JsonObj findDataById() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return cerDetaildataImportInfoService.findById(id);
    }

    @PostMapping("updateRecord")
    public JsonObj updateRecord() {
        PageData pageData = getPageData();
        return cerDetaildataImportInfoService.updateRecord(pageData);
    }

    @PostMapping("execFileParse")
    public JsonObj execFileParse() {
        PageData pageData = getPageData();
        return cerDetaildataImportInfoService.execFileParse(pageData);
    }
}
